package com.seed.core.builder.order;

import cn.hutool.core.util.StrUtil;
import com.seed.core.column.Column;
import com.seed.core.enumeration.Direction;
import com.seed.core.enumeration.PropertyType;
import com.seed.core.pojo.seed.Relation;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.pojo.Ordered;
import com.seed.util.SeedKit;
import com.seed.util.StrKit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderedBuilder {

    private final Seed seed; // 当前访问的模块
    private final List<String> values; // 排序配置，形如：column1=asc,cross.column2=desc,column2
    private Map<String, Direction> params; // 客户端提交的参数，形如：{column2: desc}

    public OrderedBuilder(Seed seed, List<String> values) {
        this.seed = seed;
        this.values = values;
    }

    public OrderedBuilder(Seed seed, List<String> values, Map<String, Direction> params) {
        this(seed, values);
        this.params = params;
    }

    public Map<String, Ordered> build() {
        Map<String, Ordered> ret = new HashMap<>();
        for(String search: format()) build(ret, seed, search);

        return ret;
    }

    private void build(Map<String, Ordered> parent, Seed seed, String order) {
        order = StrUtil.toUnderlineCase(order);
        String[] declareAndValues = StrKit.cutInTwo(order, '=', false);
        String declare = declareAndValues[0];
        Direction value = StrUtil.isBlank(declareAndValues[1]) ? Direction.asc : Direction.valueOf(declareAndValues[1].toLowerCase());

        Map<String, Column> columns = seed.getColumns();
        if(declare.contains(".")) {
            String[] items = StrKit.cutInTwo(declare, '.', false);
            String cross = items[0];
            String subDeclare = items[1];
            String subOrder = subDeclare + '=' + value.name();
            Relation relation = SeedKit.getRelation(seed, cross);
            Ordered ordered = parent.get(cross);
            if(ordered == null) ordered = new Ordered(relation);
            build(ordered, relation.targetSeed(), subOrder);
            parent.put(cross, ordered);
        } else {
            if("".equals(declare)) {
                Ordered ordered = parent.get(declare);
                if(ordered == null) parent.put(declare, new Ordered(PropertyType.none, value));
            } else if("*".equals(declare)) {
                Ordered ordered = parent.get(declare);
                if(ordered == null) parent.put(declare, new Ordered(PropertyType.asterisk, value));
            } else {
                Column column = columns.get(StrUtil.toUnderlineCase(declare));
                if(column == null) {
                    String subDeclare = "";
                    String subOrder = subDeclare + '=' + value.name();
                    Relation relation = SeedKit.getRelation(seed, declare);
                    Ordered ordered = parent.get(declare);
                    if(ordered == null) ordered = new Ordered(relation);
                    build(ordered, relation.targetSeed(), subOrder);
                    parent.put(declare, ordered);
                } else {
                    Ordered ordered = parent.get(declare);
                    if(ordered == null) parent.put(declare, new Ordered(column, value));
                }
            }
        }
    }

    public List<String> format() {
        List<String> ret = new ArrayList<>();
        for(String order: values) {
            String[] items = order.split("=");
            String finalColumn = items[0];
            String value = items.length > 1 ? items[1] : null;
            if(StrUtil.isBlank(value)) {
                Direction direction = params.get(finalColumn);
                if(direction != null) ret.add(String.format("%s=%s", finalColumn, direction.getValue()));
                // 客户端没提供值的忽略掉
            } else {
                ret.add(order);
            }
        }
        return ret;
    }
}
